package cn.iocoder.yudao.module.reservation.controller.admin.promotionactivity;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.reservation.controller.admin.promotionactivity.vo.*;
import cn.iocoder.yudao.module.reservation.dal.dataobject.promotionactivity.PromotionActivityDO;
import cn.iocoder.yudao.module.reservation.service.promotionactivity.PromotionActivityService;

@Tag(name = "管理后台 - 优惠活动")
@RestController
@RequestMapping("/reservation/promotion-activity")
@Validated
public class PromotionActivityController {

    @Resource
    private PromotionActivityService promotionActivityService;

    @PostMapping("/create")
    @Operation(summary = "创建优惠活动")
    @PreAuthorize("@ss.hasPermission('reservation:promotion-activity:create')")
    public CommonResult<Long> createPromotionActivity(@Valid @RequestBody PromotionActivitySaveReqVO createReqVO) {
        return success(promotionActivityService.createPromotionActivity(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新优惠活动")
    @PreAuthorize("@ss.hasPermission('reservation:promotion-activity:update')")
    public CommonResult<Boolean> updatePromotionActivity(@Valid @RequestBody PromotionActivitySaveReqVO updateReqVO) {
        promotionActivityService.updatePromotionActivity(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除优惠活动")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('reservation:promotion-activity:delete')")
    public CommonResult<Boolean> deletePromotionActivity(@RequestParam("id") Long id) {
        promotionActivityService.deletePromotionActivity(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得优惠活动")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('reservation:promotion-activity:query')")
    public CommonResult<PromotionActivityRespVO> getPromotionActivity(@RequestParam("id") Long id) {
        PromotionActivityDO promotionActivity = promotionActivityService.getPromotionActivity(id);
        return success(BeanUtils.toBean(promotionActivity, PromotionActivityRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得优惠活动分页")
    @PreAuthorize("@ss.hasPermission('reservation:promotion-activity:query')")
    public CommonResult<PageResult<PromotionActivityRespVO>> getPromotionActivityPage(@Valid PromotionActivityPageReqVO pageReqVO) {
        PageResult<PromotionActivityDO> pageResult = promotionActivityService.getPromotionActivityPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PromotionActivityRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出优惠活动 Excel")
    @PreAuthorize("@ss.hasPermission('reservation:promotion-activity:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportPromotionActivityExcel(@Valid PromotionActivityPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PromotionActivityDO> list = promotionActivityService.getPromotionActivityPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "优惠活动.xls", "数据", PromotionActivityRespVO.class,
                        BeanUtils.toBean(list, PromotionActivityRespVO.class));
    }

}